package shamir;

import java.io.IOException;
import java.math.BigInteger;
import java.security.PrivateKey;

import util.Utils;

public class shamirResolver
{
    private final static int t = 3; // number of shares for solve the secret (t
                                    // <= n)
    private BigInteger modulus;
    String[] privKeyPaths;

    public shamirResolver( String[] priv, String pub )
    {
        privKeyPaths = priv;
        try
        {
            modulus = Utils.readModulus( pub ); // common modulus from public
                                                // key of the pair
        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
    }

    public PrivateKey joinShamir()
    {
        shamirKey[] sk2 = new shamirKey[ t ];
        // System.out.println("joining...");
        try
        {
            // select t keys
            for( int i = 0; i < t; i++ )
                sk2[ i ] = Utils.readShamirKey( privKeyPaths[ i ] );
        }
        catch( Exception e )
        {
            System.err.println( "error while reading" );
        }
        // solve scheme, calculate parameter 0 (secret)
        byte[] des = shamir.calculateLagrange( sk2 );
        // System.out.println("Lagrange calculated...");
        String text = new String( des );
        // System.out.println( "Secret = " + text );
        PrivateKey privKey = Utils.findPrivate( modulus, text );
        return privKey;
    }
}
